//
//  RootViewController.m
//  电影豆瓣--完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "RootViewController.h"
#import "activityTableViewCell.h"
#import "ActModel.h"
#import "UIImageView+WebCache.h"
#import "MessageViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"活动";
    
    [self createTabel];
    [self dataHandle];
}



- (void)createTabel {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 69) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 注册重用池
    [_tableView registerClass:[activityTableViewCell class] forCellReuseIdentifier:@"cell"];
}



// 数据处理
- (void)dataHandle {
    self.modelArr = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activity" ofType:@"plist"];
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in tempArr) {
        ActModel *model = [ActModel actModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
    }
}



#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    activityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ActModel *model = _modelArr[indexPath.row];
    
    cell.titleLabel.text = model.title;
    cell.titleLabel.font = [UIFont systemFontOfSize:20];
    
    cell.timeLabel.text = [NSString stringWithFormat:@"%@ -- %@", model.beginTime, model.endTime];
    
    cell.typeLabel.text = [NSString stringWithFormat:@"类型：%@", model.category_name];
    cell.typeLabel.font = [UIFont systemFontOfSize:17];
    
    cell.addressLabel.text = model.address;
    cell.addressLabel.font = [UIFont systemFontOfSize:17];
    
    cell.label1.text = @"感兴趣:";
    cell.label1.font = [UIFont systemFontOfSize:15];
    cell.label11.text = [NSString stringWithFormat:@"%@", model.wisher_count];
    cell.label11.textColor = [UIColor redColor];
    
    cell.label2.text = @"参加:";
    cell.label2.font = [UIFont systemFontOfSize:15];
    cell.label22.text= [NSString stringWithFormat:@"%@", model.participant_count];
    cell.label22.textColor = [UIColor redColor];
    
    cell.imag1.image = [UIImage imageNamed:@"icon_catalog_blue.png"];
    cell.imag2.image = [UIImage imageNamed:@"icon_date_blue.png"];
    cell.imag3.image = [UIImage imageNamed:@"icon_spot_blue.png"];
    
    // 加载图片，用SDWebImage
    [cell.imag sd_setImageWithURL:[NSURL URLWithString:model.image]];
    
    return cell;
    
}

// 点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageViewController *messVC = [[MessageViewController alloc] init];
    // 属性传值
    messVC.model = _modelArr[indexPath.row];
    messVC.index = indexPath;
    
    // 这个地方用push,模态跳转以后navigationBar和tabbar都不在了
//    messVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:messVC animated:YES];
    [messVC release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 225;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
